package guoanmaker.provider.business.service;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.assertj.core.extractor.Extractors;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.mysql.jdbc.StandardSocketFactory;

import guoanmaker.provider.business.dao.ProvideInvoiceInfoDao;
import guoanmaker.provider.business.model.ProvideInvoice;
import guoanmaker.provider.business.model.ProvideInvoiceInfo;
import guoanmaker.provider.business.model.ProviderBusiness;
import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.JsonAnalyze;
import guoanmaker.provider.business.modelnobind.OrderInvoiceAddress;
import guoanmaker.provider.business.repository.ProvideInvoiceInfoRepository;
import guoanmaker.provider.business.repository.ProvideInvoiceRepository;
import guoanmaker.provider.business.repository.ProviderBusinessRepository;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import guoanmaker.provider.helper.common.util.OSSUnitHelper;
import guoanmaker.provider.helper.common.util.SystemHelper;
import guoanmaker.provider.helper.jpa.Criteria;
import guoanmaker.provider.helper.jpa.Restrictions;

/**
 * 
 * @ClassName: ProvideInvoiceService 
 * @Description: 服务商发票service
 * @author Yvonne_MGL
 * @date 2017年7月28日 上午9:41:34    
 */
@Service
public class ProvideInvoiceService {

	@Resource
	private ProvideInvoiceRepository provideInvoiceRepository;
	
	@Resource
	private ProvideInvoiceInfoRepository provideInvoiceInfoRepository;
	
	@Resource
	private ProvideInvoiceInfoDao provideInvoiceInfoDao;
	
	@Resource
	private ProviderBusinessRepository providerBusinessRepository;

	/**
	 * 
	 * @Description: 保存服务商发票 
	 * @param banAccountkName开户行名称
	 * @param bankAccountNumber开户行账号
	 * @param companyName公司名称
	 * @param taxpayerIdentifier纳税人识别号
	 * @param companyAddress公司注册地址
	 * @param contactNumber联系人电话
	 * @param businessLicenseFile营业执照副本扫描图片
	 * @param taxpayerCertificateFile一般纳税人资格证明图片
	 * @param businessid运营商id
	 * @param @return
	 * @author Yvonne_MGL
	 * @date 2017年7月28日 上午10:19:11    
	 * @return ReturnResult    @throws 
	 */
	@Transactional
	public ReturnResult createProvideInvoice(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, MultipartFile businessLicenseFile,
			MultipartFile taxpayerCertificateFile, String businessid,String companyNamebank,String taxpayerType) throws Exception{
		ReturnResult returnResult = new ReturnResult();
		if (businessid == null || businessid.equals("")) {
			returnResult.setKey("error");
			returnResult.setValue("服务商id不允许为空");
		} else {
			Criteria<ProvideInvoice> criteria = new Criteria<>();
			criteria.add(Restrictions.like("businessid", businessid, true));
			List<ProvideInvoice> list = provideInvoiceRepository.findAll(criteria);
			if (list != null && list.size() > 0) {
				returnResult.setKey("error");
				returnResult.setValue("服务商发票已存在");
			} else {
				ProviderBusiness business=providerBusinessRepository.findOne(businessid);
				if(business==null){
					returnResult.setKey("error");
					returnResult.setValue("服务商不存在");
				}else{
					ProvideInvoice provideInvoice = new ProvideInvoice();
					provideInvoice.setBanAccountkName(banAccountkName);
					provideInvoice.setBankAccountNumber(bankAccountNumber);
					provideInvoice.setCompanyName(companyName);
					provideInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
					provideInvoice.setCompanyAddress(companyAddress);
					provideInvoice.setContactNumber(contactNumber);
					provideInvoice.setBusinessid(businessid);
					provideInvoice.setCompanyNamebank(companyNamebank);
					provideInvoice.setTaxpayerType(taxpayerType);
					if (businessLicenseFile != null && businessLicenseFile.getSize() > 0) {
						String fileName = businessLicenseFile.getOriginalFilename();
						String fileExtension = fileName.substring(fileName.lastIndexOf("."));
						OSSClient client = OSSUnitHelper.getOSSClient();
						OSSUnitHelper.uploadObjectOSS(client, businessLicenseFile,
								ApiController.INVOICE_PIC_URL + businessid + "/", "businessLicense" + fileExtension);
						String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + businessid + "/",
								"businessLicense" + fileExtension);
						String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date();
						String uString=urlstr.replaceAll("http", "https");
						provideInvoice.setBusinessLicensePic(uString);
					}
					if (taxpayerCertificateFile != null && taxpayerCertificateFile.getSize() > 0) {
						String fileName = taxpayerCertificateFile.getOriginalFilename();
						String fileExtension = fileName.substring(fileName.lastIndexOf("."));
						OSSClient client = OSSUnitHelper.getOSSClient();
						OSSUnitHelper.uploadObjectOSS(client, taxpayerCertificateFile,
								ApiController.INVOICE_PIC_URL + businessid + "/", "taxpayerCertificate" + fileExtension);
						String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + businessid + "/",
								"taxpayerCertificate" + fileExtension);
						String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date();
						String uString=urlstr.replaceAll("http", "https");
						provideInvoice.setTaxpayerCertificatePic(uString);
					}
					
					String file1="";
					String file2="";
					if(provideInvoice.getBusinessLicensePic()!=null){
						file1=URLEncoder.encode(provideInvoice.getBusinessLicensePic(), "UTF-8");
					}
					if(provideInvoice.getTaxpayerCertificatePic()!=null){
						file2=URLEncoder.encode(provideInvoice.getTaxpayerCertificatePic(), "UTF-8");
					}
					
					String url=ApiController.PAY_URL+"add_update/company";
					String param="systemCode=10&originalId="+businessid+"&companyName="+
							URLEncoder.encode(business.getBusinessName(),"UTF-8")+"&companyTitle="+URLEncoder.encode(business.getCompanyName(),"UTF-8")+
							"&bankName="+URLEncoder.encode(provideInvoice.getBanAccountkName(),"UTF-8")+"&bankNumber="+provideInvoice.getBankAccountNumber()+
					"&invoiceTitle="+URLEncoder.encode(provideInvoice.getCompanyName(),"UTF-8")+"&address="+URLEncoder.encode(provideInvoice.getCompanyAddress(),"UTF-8")+
					"&companyName1="+URLEncoder.encode(provideInvoice.getCompanyNamebank(),"UTF-8")+"&phone="+URLEncoder.encode(provideInvoice.getContactNumber(),"UTF-8")+
					"&type_ratepay="+URLEncoder.encode(provideInvoice.getTaxpayerType(),"UTF-8")+
					"&invoiceInfo1="+URLEncoder.encode(provideInvoice.getTaxpayerIdentifier(),"UTF-8")+"&invoiceInfo2="+file1+"&invoiceInfo3="+file2+"&isJava=1";
					String result=SystemHelper.httpGetHelper(url, param);
					if(result.indexOf("SUCCESS")!=-1){
						provideInvoiceRepository.save(provideInvoice);
						returnResult.setKey("success");
						returnResult.setValue("保存服务商发票成功");
					}else{
						returnResult.setKey("error");
						returnResult.setValue("保存务商发票至财务平台失败");
					}
					
				}
				
			}
		}
		return returnResult;
	}
	
	@Transactional
	public ReturnResult createProvideInvoiceForPC(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, String businessLicenseFile,
			String taxpayerCertificateFile, String businessid,String companyNamebank,String taxpayerType) throws Exception{
		ReturnResult returnResult = new ReturnResult();
		if (businessid == null || businessid.equals("")) {
			returnResult.setKey("error");
			returnResult.setValue("服务商id不允许为空");
		} else {
			Criteria<ProvideInvoice> criteria = new Criteria<>();
			criteria.add(Restrictions.like("businessid", businessid, true));
			List<ProvideInvoice> list = provideInvoiceRepository.findAll(criteria);
			if (list != null && list.size() > 0) {
				returnResult.setKey("error");
				returnResult.setValue("服务商发票已存在");
			} else {
				ProviderBusiness business=providerBusinessRepository.findOne(businessid);
				if(business==null){
					returnResult.setKey("error");
					returnResult.setValue("服务商不存在");
				}else{
					ProvideInvoice provideInvoice = new ProvideInvoice();
					provideInvoice.setBanAccountkName(banAccountkName);
					provideInvoice.setBankAccountNumber(bankAccountNumber);
					provideInvoice.setCompanyName(companyName);
					provideInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
					provideInvoice.setCompanyAddress(companyAddress);
					provideInvoice.setContactNumber(contactNumber);
					provideInvoice.setBusinessid(businessid);
					provideInvoice.setCompanyNamebank(companyNamebank);
					provideInvoice.setTaxpayerType(taxpayerType);
					String uString="";
					String uString2="";
					if(businessLicenseFile!=null){
						if(!businessLicenseFile.startsWith("https")){
							uString=businessLicenseFile.replaceAll("http", "https");
						}else{
							uString=businessLicenseFile;
						}
						
					}
					provideInvoice.setBusinessLicensePic(uString);
					if(taxpayerCertificateFile!=null){
						if(!taxpayerCertificateFile.startsWith("https")){
							uString2=businessLicenseFile.replaceAll("http", "https");
						}else{
							uString2=businessLicenseFile;
						}
						
					}
					provideInvoice.setTaxpayerCertificatePic(uString2);

					String file1="";
					String file2="";
					if(provideInvoice.getBusinessLicensePic()!=null){
						file1=URLEncoder.encode(provideInvoice.getBusinessLicensePic(), "UTF-8");
					}
					if(provideInvoice.getTaxpayerCertificatePic()!=null){
						file2=URLEncoder.encode(provideInvoice.getTaxpayerCertificatePic(), "UTF-8");
					}
					
					String url=ApiController.PAY_URL+"add_update/company";
					String param="systemCode=10&originalId="+businessid+"&companyName="+
							URLEncoder.encode(business.getBusinessName(),"UTF-8")+"&companyTitle="+URLEncoder.encode(business.getCompanyName(),"UTF-8")+
							"&bankName="+URLEncoder.encode(provideInvoice.getBanAccountkName(),"UTF-8")+"&bankNumber="+provideInvoice.getBankAccountNumber()+
					"&invoiceTitle="+URLEncoder.encode(provideInvoice.getCompanyName(),"UTF-8")+"&address="+URLEncoder.encode(provideInvoice.getCompanyAddress(),"UTF-8")+
					"&companyName1="+URLEncoder.encode(provideInvoice.getCompanyNamebank(),"UTF-8")+"&phone="+URLEncoder.encode(provideInvoice.getContactNumber(),"UTF-8")+
					"&type_ratepay="+URLEncoder.encode(provideInvoice.getTaxpayerType(),"UTF-8")+
					"&invoiceInfo1="+URLEncoder.encode(provideInvoice.getTaxpayerIdentifier(),"UTF-8")+"&invoiceInfo2="+file1+"&invoiceInfo3="+file2+"&isJava=1";
					String result=SystemHelper.httpGetHelper(url, param);
					if(result.indexOf("SUCCESS")!=-1){
						provideInvoiceRepository.save(provideInvoice);
						returnResult.setKey("success");
						returnResult.setValue("保存服务商发票成功");
					}else{
						returnResult.setKey("error");
						returnResult.setValue("保存服务商发票至财务平台失败");
					}
					
				}
				
			}
		}
		return returnResult;
	}

	/**
	 * 
	 * @Description: 修改服务商发票 
	 * @param banAccountkName开户行名称
	 * @param bankAccountNumber开户行账号
	 * @param companyName公司名称
	 * @param taxpayerIdentifier纳税人识别号
	 * @param companyAddress公司注册地址
	 * @param contactNumber联系人电话
	 * @param businessLicenseFile营业执照副本扫描图片
	 * @param taxpayerCertificateFile一般纳税人资格证明图片
	 * @param id 
	 * @param @return
	 * @author Yvonne_MGL 
	 * @date 2017年7月28日 下午2:00:21     
	 * @return ReturnResult    
	 * @throws 
	 */
	@Transactional
	public ReturnResult updateProvideInvoice(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, MultipartFile businessLicenseFile,
			MultipartFile taxpayerCertificateFile, String id,String companyNamebank,String taxpayerType) throws Exception{
		ReturnResult returnResult = new ReturnResult();
		if (id == null || id.equals("")) {
			returnResult.setKey("error");
			returnResult.setValue("id不允许为空");
		} else {
			ProvideInvoice provideInvoice = provideInvoiceRepository.findOne(id);
			if (provideInvoice == null) {
				returnResult.setKey("error");
				returnResult.setValue("发票不存在");
			} else {
				ProviderBusiness business=providerBusinessRepository.findOne(provideInvoice.getBusinessid());
				if(business==null){
					returnResult.setKey("error");
					returnResult.setValue("服务商不存在");
				}else{
					if (banAccountkName != null && !banAccountkName.equals("")) {
						provideInvoice.setBanAccountkName(banAccountkName);
					}
					if (bankAccountNumber != null && !bankAccountNumber.equals("")) {
						provideInvoice.setBankAccountNumber(bankAccountNumber);
					}
					if (companyName != null && !companyName.equals("")) {
						provideInvoice.setCompanyName(companyName);
					}
					if (taxpayerIdentifier != null && !taxpayerIdentifier.equals("")) {
						provideInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
					}
					if (companyAddress != null && !companyAddress.equals("")) {
						provideInvoice.setCompanyAddress(companyAddress);
					}
					if (contactNumber != null && !contactNumber.equals("")) {
						provideInvoice.setContactNumber(contactNumber);
					}
					if(companyNamebank!=null&&!companyNamebank.equals("")){
						provideInvoice.setCompanyNamebank(companyNamebank);
					}
					if(taxpayerType!=null&&!taxpayerType.equals("")){
						provideInvoice.setTaxpayerType(taxpayerType);
					}
					if (businessLicenseFile != null && businessLicenseFile.getSize() > 0) {
						String fileName = businessLicenseFile.getOriginalFilename();
						String fileExtension = fileName.substring(fileName.lastIndexOf("."));
						OSSClient client = OSSUnitHelper.getOSSClient();
						OSSUnitHelper.uploadObjectOSS(client, businessLicenseFile,
								ApiController.INVOICE_PIC_URL + provideInvoice.getBusinessid() + "/", "businessLicense" + fileExtension);
						String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + provideInvoice.getBusinessid() + "/",
								"businessLicense" + fileExtension);
						String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date();
						provideInvoice.setBusinessLicensePic(urlstr);
					}
					if (taxpayerCertificateFile != null && taxpayerCertificateFile.getSize() > 0) {
						String fileName = taxpayerCertificateFile.getOriginalFilename();
						String fileExtension = fileName.substring(fileName.lastIndexOf("."));
						OSSClient client = OSSUnitHelper.getOSSClient();
						OSSUnitHelper.uploadObjectOSS(client, taxpayerCertificateFile,
								ApiController.INVOICE_PIC_URL + provideInvoice.getBusinessid() + "/", "taxpayerCertificate" + fileExtension);
						String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + provideInvoice.getBusinessid() + "/",
								"taxpayerCertificate" + fileExtension);
						String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date();
						provideInvoice.setTaxpayerCertificatePic(urlstr);
					}
					String file1="";
					String file2="";
					if(provideInvoice.getBusinessLicensePic()!=null){
						file1=URLEncoder.encode(provideInvoice.getBusinessLicensePic(), "UTF-8");
					}
					if(provideInvoice.getTaxpayerCertificatePic()!=null){
						file2=URLEncoder.encode(provideInvoice.getTaxpayerCertificatePic(), "UTF-8");
					}
					String url=ApiController.PAY_URL+"add_update/company";
					String param="systemCode=10&originalId="+business.getId()+"&companyName="+
							URLEncoder.encode(business.getBusinessName(),"UTF-8")+"&companyTitle="+URLEncoder.encode(business.getCompanyName(),"UTF-8")+
							"&bankName="+URLEncoder.encode(provideInvoice.getBanAccountkName(),"UTF-8")+"&bankNumber="+provideInvoice.getBankAccountNumber()+
					"&invoiceTitle="+URLEncoder.encode(provideInvoice.getCompanyName(),"UTF-8")+"&address="+URLEncoder.encode(provideInvoice.getCompanyAddress(),"UTF-8")+
					"&companyName1="+URLEncoder.encode(provideInvoice.getCompanyNamebank(),"UTF-8")+"&phone="+URLEncoder.encode(provideInvoice.getContactNumber(),"UTF-8")+
					"&type_ratepay="+URLEncoder.encode(provideInvoice.getTaxpayerType(),"UTF-8")+
					"&invoiceInfo1="+URLEncoder.encode(provideInvoice.getTaxpayerIdentifier(),"UTF-8")+"&invoiceInfo2="+file1+"&invoiceInfo3="+file2+"&isJava=1";
					String result=SystemHelper.httpGetHelper(url, param);
					
					if(result.indexOf("SUCCESS")!=-1){
						provideInvoiceRepository.save(provideInvoice);
						returnResult.setKey("success");
						returnResult.setValue("保存服务商发票成功");
					}else{
						returnResult.setKey("error");
						returnResult.setValue("保存服务商发票至财务平台失败");
					}
					
				}
			}
		}
		return returnResult;
	}
	
	@Transactional
	public ReturnResult updateProvideInvoiceForPC(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, String businessLicenseFile,
			String taxpayerCertificateFile, String id,String companyNamebank,String taxpayerType) throws Exception{
		ReturnResult returnResult = new ReturnResult();
		if (id == null || id.equals("")) {
			returnResult.setKey("error");
			returnResult.setValue("id不允许为空");
		} else {
			ProvideInvoice provideInvoice = provideInvoiceRepository.findOne(id);
			if (provideInvoice == null) {
				returnResult.setKey("error");
				returnResult.setValue("发票不存在");
			} else {
				ProviderBusiness business=providerBusinessRepository.findOne(provideInvoice.getBusinessid());
				if(business==null){
					returnResult.setKey("error");
					returnResult.setValue("服务商不存在");
				}else{
					if (banAccountkName != null && !banAccountkName.equals("")) {
						provideInvoice.setBanAccountkName(banAccountkName);
					}
					if (bankAccountNumber != null && !bankAccountNumber.equals("")) {
						provideInvoice.setBankAccountNumber(bankAccountNumber);
					}
					if (companyName != null && !companyName.equals("")) {
						provideInvoice.setCompanyName(companyName);
					}
					if (taxpayerIdentifier != null && !taxpayerIdentifier.equals("")) {
						provideInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
					}
					if (companyAddress != null && !companyAddress.equals("")) {
						provideInvoice.setCompanyAddress(companyAddress);
					}
					if (contactNumber != null && !contactNumber.equals("")) {
						provideInvoice.setContactNumber(contactNumber);
					}
					if(companyNamebank!=null&&!companyNamebank.equals("")){
						provideInvoice.setCompanyNamebank(companyNamebank);
					}
					if(taxpayerType!=null&&!taxpayerType.equals("")){
						provideInvoice.setTaxpayerType(taxpayerType);
					}
					if (businessLicenseFile != null && !businessLicenseFile.equals("")) {
						if(!businessLicenseFile.startsWith("https")){
							String uString=businessLicenseFile.replaceAll("http", "https");
							provideInvoice.setBusinessLicensePic(uString);
						}else{
							provideInvoice.setBusinessLicensePic(businessLicenseFile);
						}
					}
					if (taxpayerCertificateFile != null && !taxpayerCertificateFile.equals("")) {
						if(!taxpayerCertificateFile.startsWith("https")){
							String uString=taxpayerCertificateFile.replaceAll("http", "https");
							provideInvoice.setTaxpayerCertificatePic(uString);
						}else{
							provideInvoice.setTaxpayerCertificatePic(taxpayerCertificateFile);
						}
					}
					
					String file1="";
					String file2="";
					if(provideInvoice.getBusinessLicensePic()!=null){
						file1=URLEncoder.encode(provideInvoice.getBusinessLicensePic(), "UTF-8");
					}
					if(provideInvoice.getTaxpayerCertificatePic()!=null){
						file2=URLEncoder.encode(provideInvoice.getTaxpayerCertificatePic(), "UTF-8");
					}
					
					String url=ApiController.PAY_URL+"add_update/company";
					String param="systemCode=10&originalId="+business.getId()+"&companyName="+
							URLEncoder.encode(business.getBusinessName(),"UTF-8")+"&companyTitle="+URLEncoder.encode(business.getCompanyName(),"UTF-8")+
							"&bankName="+URLEncoder.encode(provideInvoice.getBanAccountkName(),"UTF-8")+"&bankNumber="+provideInvoice.getBankAccountNumber()+
					"&invoiceTitle="+URLEncoder.encode(provideInvoice.getCompanyName(),"UTF-8")+"&address="+URLEncoder.encode(provideInvoice.getCompanyAddress(),"UTF-8")+
					"&companyName1="+URLEncoder.encode(provideInvoice.getCompanyNamebank(),"UTF-8")+"&phone="+URLEncoder.encode(provideInvoice.getContactNumber(),"UTF-8")+
					"&type_ratepay="+URLEncoder.encode(provideInvoice.getTaxpayerType(),"UTF-8")+
					"&invoiceInfo1="+URLEncoder.encode(provideInvoice.getTaxpayerIdentifier(),"UTF-8")+"&invoiceInfo2="+file1+"&invoiceInfo3="+file2+"&isJava=1";
					String result=SystemHelper.httpGetHelper(url, param);
					
					if(result.indexOf("SUCCESS")!=-1){
						provideInvoiceRepository.save(provideInvoice);
						returnResult.setKey("success");
						returnResult.setValue("保存服务商发票成功");
					}else{
						returnResult.setKey("error");
						returnResult.setValue("保存服务商发票至财务平台失败");
					}
					
				}
			}
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 根据服务商id查询发票信息以及账户信息 
	* @param businessid 服务商id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午4:58:58     
	* @return Map    
	* @throws 
	 */
	public Map findInvoiceInfos(String businessid){
		return provideInvoiceInfoDao.findInvoiceInfos(businessid);
	}
	
	/**
	 * 
	* @Description: 服务商提交开发票申请 
	* @param invoiceid 发票基础信息id
	* @param invoiceType 发票类型  0为增值税普通发票    1为增值税专用发票
	* @param amountOfMoney 开票金额
	* @param mailingAddress 邮寄地址
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:27:35     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult createProvideInvoiceInfo(String invoiceid,String invoiceType,Double amountOfMoney,String mailingAddress)throws Exception{
		ReturnResult returnResult = new ReturnResult();
		ProvideInvoice provideInvoice=provideInvoiceRepository.findOne(invoiceid);
		if(provideInvoice==null){
			returnResult.setKey("error");
			returnResult.setValue("发票基本信息不存在");
		}else{
			
			String url=ApiController.PAY_URL+"querycompany";
			String param="systemCode=10&originalId="+provideInvoice.getBusinessid()+"&isJava=1";
			String json=SystemHelper.httpGetHelper(url, param);
			
			Double min=JsonAnalyze.getMinInvoice(json);
			if(amountOfMoney>provideInvoiceInfoDao.findsumNotInvoicing(provideInvoice.getBusinessid()).doubleValue()){
				returnResult.setKey("error");
				returnResult.setValue("要开发票金额超过未开发票金额");
			}else if(min>amountOfMoney){
				returnResult.setKey("error");
				returnResult.setValue("开票金额不得小于最小开票金额");
			}else{
				
				ProvideInvoiceInfo provideInvoiceInfo=new ProvideInvoiceInfo();
				provideInvoiceInfo.setInvoiceid(invoiceid);
				provideInvoiceInfo.setBusinessid(provideInvoice.getBusinessid());
				provideInvoiceInfo.setInvoiceType(invoiceType);
				provideInvoiceInfo.setAmountOfMoney(new BigDecimal(amountOfMoney));
				provideInvoiceInfo.setMailingAddress(mailingAddress);
				provideInvoiceInfo.setInvoiceState("0");
				provideInvoiceInfo.setApplyTime(new Date());
				provideInvoiceInfo.setChannel("2");
				provideInvoiceInfoRepository.save(provideInvoiceInfo);
				//int amount=Integer.parseInt((amountOfMoney*100+"").substring(0, (amountOfMoney*100+"").indexOf(".")));
				String aaDouble=new DecimalFormat("#").format(amountOfMoney*100);
				Long amount=Long.parseLong(aaDouble);
				String url1=ApiController.PAY_URL+"askInvoice";
				String param1="systemCode=10&originalId="+provideInvoiceInfo.getBusinessid()+"&isJava=1&types=2&invoiceId="+provideInvoiceInfo.getId()+"&type="
						+ provideInvoiceInfo.getInvoiceType()+"&amount="+amount+"&address="+URLEncoder.encode(provideInvoiceInfo.getMailingAddress(),"UTF-8");
				System.out.println(param1);
				String result=SystemHelper.httpGetHelper(url1, param1);
				
				if(result.indexOf("SUCCESS")!=-1){
					returnResult.setKey("success");
					returnResult.setValue("服务商已提交开发票申请");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("提交财务平台开票失败");
					provideInvoiceInfoRepository.delete(provideInvoiceInfo.getId());
				}
				
			}
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 确定给服务商开发票 开票直接申请财务平台，去掉审核
	* @param id 已提交的开发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:45:00     
	* @return ReturnResult    
	* @throws 
	 */
	/*
	@Transactional
	public ReturnResult confirmInvoicing(String id)throws Exception{
		ReturnResult returnResult = new ReturnResult();
		ProvideInvoiceInfo provideInvoiceInfo=provideInvoiceInfoRepository.findOne(id);
		if(provideInvoiceInfo==null){
			returnResult.setKey("error");
			returnResult.setValue("已提交发票不存在");
		}else{
			if(provideInvoiceInfo.getInvoiceState()!=null&&provideInvoiceInfo.getInvoiceState().equals("1")){
				returnResult.setKey("error");
				returnResult.setValue("发票已开");
			}else{
				
				int amount=Integer.parseInt((provideInvoiceInfo.getAmountOfMoney().doubleValue()*100+"").substring(0, (provideInvoiceInfo.getAmountOfMoney().doubleValue()*100+"").indexOf(".")));
				
				String url=ApiController.PAY_URL+"askInvoice";
				String param="systemCode=10&originalId="+provideInvoiceInfo.getBusinessid()+"&isJava=1&type="
						+ provideInvoiceInfo.getInvoiceType()+"&amount="+amount+"&address="+URLEncoder.encode(provideInvoiceInfo.getMailingAddress(),"UTF-8");
				String result=SystemHelper.httpGetHelper(url, param);
				
				if(result.indexOf("SUCCESS")!=-1){
					provideInvoiceInfo.setInvoiceState("1");
					provideInvoiceInfo.setBillingTime(new Date());
					provideInvoiceInfoRepository.save(provideInvoiceInfo);
					returnResult.setKey("success");
					returnResult.setValue("确认开票成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("财务平台开票成功");
				}
				
				
			}
		}
		
		return returnResult;
	}
	*/
	/**
	 * 
	* @Description: 根据id查询服务商发票基础信息 
	* @param @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午4:55:21     
	* @return ProvideInvoice    
	* @throws 
	 */
	public ProvideInvoice findProvideInvoice(String businessid){
		return provideInvoiceInfoDao.findProvideInvoice(businessid);
	}
	
	/**
	 * 
	* @Description:查询订单发票详细信息
	* @param @param orderid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月30日 上午11:38:24     
	* @return OrderInvoiceAddress    
	* @throws 
	 */
	public OrderInvoiceAddress getOrderInvoice(String orderid){
		return provideInvoiceInfoDao.getOrderInvoice(orderid);
	}

}
